package com.gufe.service.user.impl.mp;

import com.gufe.entity.PrecollectMember;
import com.gufe.mapper.user.mp.PrecollectMemberMapper;
import com.gufe.service.user.mp.PrecollectMemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Slf4j
@Service
public class PrecollectMemberServiceImpl implements PrecollectMemberService {

    @Autowired
    private PrecollectMemberMapper precollectMemberMapper;

    @Override
    @Transactional
    public void submitPrecollectMember(PrecollectMember precollectMember) throws Exception {
        try {
            // 设置创建和更新时间
            LocalDateTime now = LocalDateTime.now();
            precollectMember.setCreatedAt(now);
            precollectMember.setUpdatedAt(now);
            
            // 打印插入前的数据
            log.info("Inserting precollect member: {}", precollectMember);
            
            int result = precollectMemberMapper.insert(precollectMember);
            
            if (result <= 0) {
                throw new Exception("插入数据失败");
            }
            
            log.info("Insert success, affected rows: {}", result);
        } catch (Exception e) {
            log.error("Insert precollect member failed", e);
            throw e;
        }
    }
}
